<template>
  <div class="app-container" style="padding: 0 2px 20px">
    <el-row
      :gutter="10"
      style="margin-bottom: 9px; display: flex; justify-content: flex-end"
    >
      <el-col :span="1.5">
        <el-button
          v-if="optBtn"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <template v-for="list in recordsList">
      <el-descriptions
        :column="3"
        style="box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1); margin-bottom: 8px"
        v-if="recordsList.length != 0"
        size="small"
        :colon="false"
      >
        <el-descriptions-item label="创建时间:" style="margin-left: 5px">{{
          list.creationTime
        }}</el-descriptions-item>
        <el-descriptions-item label="日志内容:" style="margin-left: 5px">{{
          list.logContent
        }}</el-descriptions-item>
        <el-descriptions-item label="创建人:">{{
          list.creationName
        }}</el-descriptions-item>
        <el-descriptions-item label="附件:">
          <file-view
            :type="showInfo"
            :businessId="list.id"
            :businessCode="businessRecords"
            @change="getFile"
          ></file-view>
        </el-descriptions-item>
        <el-descriptions-item style="margin-left: 5px">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="margin: 0px 0px 10px 860px"
            @click="handleUpdate(list)"
            title="编辑"
          ></el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="margin-left: 10px"
            @click="handleDelete(list.id)"
            title="删除"
          ></el-button>
        </el-descriptions-item>
      </el-descriptions>
    </template>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div
      class="el-table__empty-block"
      style="width: 1170px; height: 100%"
      v-if="recordsList.length == 0"
    >
      <span class="el-table__empty-text" style="font-size: 16px">暂无数据</span>
    </div>
    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="创建时间" prop="creationTime">
              <el-input
                v-model="form.creationTime"
                disabled="disabled"
                style="width: 220px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item
              label="创建人"
              prop="creationName"
              label-width="170px"
            >
              <el-input v-model="form.creationName" disabled="disabled" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item
          label="日志内容"
          prop="logContent"
          style="margin-bottom: 20px"
        >
          <el-input
            v-model="form.logContent"
            type="textarea"
            placeholder="请输入内容"
            :rows="4"
            :min-height="192"
            :disabled="disabled"
            maxlength="500"
          />
        </el-form-item>
        <el-form-item label="附件">
          <file-view
            :key="refreshKey"
            :type="fileType"
            :businessId="cordId"
            :businessCode="businessRecords"
            @change="getFile"
          ></file-view>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible" append-to-body>
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import { listLog, addLog, getLog, updateLog, delLog } from "@/api/project/log";
import FileView from "@/components/FileView/index";
export default {
  name: "Records",
  components: { FileView },
  dicts: ["tracking_stage", "enterprise_records"],
  props: {
    bId: {
      type: String,
      default: "",
    },
    optBtn: {
      type: Boolean,
      default: true,
    },
    obj: {
      type: Object,
      default: null,
    },
    projectId: {
      type: String,
      required: true,
    },
    processId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      userName: "",
      fileType: 0,
      showInfo: 2,
      refreshKey: 0,
      disabled: false,
      isShowEdit: true,
      uuid: "",
      cordId: "",
      isShow: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      recordsList: [],
      // 弹出层标题
      title: "",
      dialogVisible: false,
      dialogImageUrl: "",
      businessRecords: "serviceLog",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        projectId: null,
        processId: null,
        businessId: null,
        creationTime: null,
        creationName: null,
        logContent: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        logContent: [
          { required: true, message: "日志内容不能为空", trigger: "blur" },
          { max: 500, message: "日志内容不能超过500", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.userName = this.$store.state.user.name;
    this.getList();
  },
  watch: {
    processId: {
      handler(newVal) {
        if (this.form) {
          this.form.processId = newVal;
        }
        if (newVal) {
          this.getList();
        } else {
          this.logList = [];
        }
      },
      immediate: true, // 组件创建时就执行一次
    },
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.recordsList = [];
      this.queryParams.projectId = this.projectId;
      this.queryParams.processId = this.processId;
      listLog(this.queryParams).then(response => {
        this.recordsList = response.rows;
        this.total = response.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handlePictureCardPreview(url) {
      this.dialogImageUrl = url;
      this.dialogVisible = true;
    },
    getFile(data) {
      console.log(JSON.stringify(data));
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: this.projectId,
        processId: this.processId,
        businessId: null,
        creationTime: null,
        creationName: null,
        logContent: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.uuid = this.guid();
      this.cordId = this.uuid;
      this.isShow = false;
      this.disabled = false;
      this.form.creationName = this.$store.state.user.name;
      this.form.creationTime = this.showDate();
      this.form.businessId = this.bId;
      this.fileType = 0;
      this.open = true;
      this.title = "新增跟进记录";
      this.refreshKey++;
    },
    formatterType(value) {
      var type = "";
      this.dict.type.enterprise_records.forEach(item => {
        if (item.value == value) type = item.label;
      });
      return type;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.fileType = 1;
      this.disabled = false;
      this.isShow = false;
      this.title = "修改跟进记录";
      const id = row.id || this.ids;
      this.cordId = row.id;
      getLog(id).then(response => {
        this.form = response.data;
        this.open = true;
      });
      this.refreshKey++;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.projectId = this.projectId;
          this.form.processId = this.processId;
          this.form.stageName = this.stageName;
          if (this.form.id != null) {
            updateLog(this.form).then(response => {
              this.refreshKey++;
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("change");
              this.getList();
            });
          } else {
            this.form.id = this.uuid;
            addLog(this.form).then(response => {
              this.refreshKey++;
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("change");
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row;
      this.$modal
        .confirm("是否确认删除该条跟进记录信息？")
        .then(function () {
          return delLog(ids);
        })
        .then(() => {
          this.getList();
          this.$emit("change");
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    showDate() {
      var mydate = new Date();
      var str = "" + mydate.getFullYear() + "-";
      str +=
        mydate.getMonth() + 1 <= 9
          ? "0" + (mydate.getMonth() + 1) + "-"
          : mydate.getMonth() + 1 + "-";
      str +=
        mydate.getDate() <= 9
          ? "0" + mydate.getDate() + " "
          : mydate.getDate() + " ";
      str +=
        mydate.getHours() <= 9
          ? "0" + mydate.getHours() + ":"
          : mydate.getHours() + ":";
      str +=
        mydate.getMinutes() <= 9
          ? "0" + mydate.getMinutes() + ":"
          : mydate.getMinutes() + ":";
      str +=
        mydate.getSeconds() <= 9
          ? "0" + mydate.getSeconds()
          : mydate.getSeconds();
      return str;
    },
    guid() {
      function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
      }
      return (
        S4() +
        S4() +
        "-" +
        S4() +
        "-" +
        S4() +
        "-" +
        S4() +
        "-" +
        S4() +
        S4() +
        S4()
      );
    },
  },
};
</script>
<style scoped>
::v-deep .el-descriptions-item__container {
  margin-left: 8px;
  margin-top: 5px;
}
.el-descriptions__header {
  padding: 0px;
}
.el-descriptions-item__label.has-colon::after {
  content: "";
}
.el-descriptions-item__container {
  margin-left: 5px;
}
</style>
